 package cn.edu.ujn.controller;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.dao.BaseDict;
import cn.edu.ujn.dao.Customer;
import cn.edu.ujn.dao.User;
import cn.edu.ujn.service.IBaseDictService;
import cn.edu.ujn.service.ICustomerService;
import cn.edu.ujn.utils.Page;
//成为控制器类
@Controller
@RequestMapping("/customer")
 public class CustomerController {
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IBaseDictService baseDictService;
    
//------@Value读取resource.properties文件中的值（先加载属性文件）
    // 客户来源
    @Value("${customer.from.type}")
    private String FROM_TYPE;
    // 客户所属行业
    @Value("${customer.industry.type}")
    private String INDUSTRY_TYPE;
    // 客户级别
    @Value("${customer.level.type}")
    private String LEVEL_TYPE;
//----
    @RequestMapping("/list.action")
    public String searchByCondition(@RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "9") Integer rows, String custName, String custSource, String custIndustry,
            String custLevel, Model model) throws JspException {
        //返回分页对象
        Page<Customer> customers = customerService.selectByCondintion(page, rows, custName, custSource, custIndustry,
                custLevel);
        //保存数据到前端
        model.addAttribute("page", customers);

        // 客户来源
        List<BaseDict> fromType = baseDictService.findBaseDictByTypeCode(FROM_TYPE);
        // 客户所属行业
        List<BaseDict> industryType = baseDictService.findBaseDictByTypeCode(INDUSTRY_TYPE);
        // 客户级别
        List<BaseDict> levelType = baseDictService.findBaseDictByTypeCode(LEVEL_TYPE);
        
        
        model.addAttribute("fromType", fromType);
        model.addAttribute("industryType", industryType);
        model.addAttribute("levelType", levelType);
        //将搜索栏的选择值或者输入值固定下来
        model.addAttribute("custSource", custSource);
        model.addAttribute("custName", custName);
        model.addAttribute("custIndustry", custIndustry);
        model.addAttribute("custLevel", custLevel);

        return "customer";
    }
    //创建客户
    @RequestMapping("/create.action")
    @ResponseBody//这里将返回值转为json格式的数据,让jsp前端通过调用
    public String creatCustomer(Customer customer,HttpSession session) {
        
        //设置创建者id
        User creat_user=(User) session.getAttribute("USER_SESSION");
        customer.setCust_create_id(creat_user.getUser_id());
        //设置创建时间
        Timestamp cust_createtime = new Timestamp(new Date().getTime());
        customer.setCust_createtime(cust_createtime);
        
        //创建客户
        int creat = customerService.creatCustomer(customer);
          if(creat > 0){    //创建成功      
                return "OK";//必须返回OK,成功后jsp页面对data中数据做OK判断
            }else{
                return "FAIL";          
            }
        
    }
    //得到id用户的信息
    @RequestMapping("/getCustomerById.action")
    @ResponseBody//这里将返回值转为json格式的数据,让jsp前端通过调用
    public Customer getCustomerById(Integer id) {
        return customerService.getCustomerById(id);
    }
    //更改客户
    @RequestMapping("/update.action")
    @ResponseBody//这里将返回值转为json格式的数据,让jsp前端通过调用
    public String updateCustomer(Customer customer) {
         int update = customerService.updateCustomer(customer);
         if(update > 0){    //更新成功      
                return "OK";//必须返回OK,成功后jsp页面对data中数据做OK判断
            }else{
                return "FAIL";          
            }
    }
     // 删除客户
    @RequestMapping("/delete.action")
    @ResponseBody//这里将String 为json格式的数据,让jsp前端通过调用
    public String customerDelete(Integer id) {
        int rows = customerService.deleteCustomer(id);
        if(rows > 0){   //删除成功      
            return "OK";//必须返回OK,成功后jsp页面对data中数据做OK判断
        }else{
            return "FAIL";          
        }
    }

}
